using IRepository;
using IServices;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using YL.Core.Dto;
using YL.Core.Entity;
using YL.Core.Entity.Meter;
using YL.Utils.Extensions;
using YL.Utils.Json;
using YL.Utils.Pub;
using YL.Utils.Table;

namespace Services
{
    public class Meter_DeviceInfoServices : BaseServices<DeviceInfo>, IMeter_DeviceInfoServices
    {
        private readonly IMeter_DeviceInfoRepository _repository;
        private readonly ISys_rolemenuServices _rolemenuServices;
        private readonly ISys_menuServices _menuServices;
        private readonly SqlSugarClient _client;

        public Meter_DeviceInfoServices(ISys_menuServices menuServices, ISys_rolemenuServices rolemenuServices, SqlSugarClient client, IMeter_DeviceInfoRepository repository) : base(repository)
        {
            _repository = repository;
            _client = client;
            _rolemenuServices = rolemenuServices;
            _menuServices = menuServices;
        }

        public string PageList(Bootstrap.BootstrapParams bootstrap, long userId)
        {
            int totalNumber = 0;
            if (bootstrap.offset != 0)
            {
                bootstrap.offset = bootstrap.offset / bootstrap.limit + 1;
            }

            var query = _client.Queryable<DeviceInfo, ClientInfo, Sys_user>
                ((s, c, u) => new object[] {
                   JoinType.Left,s.ClientId==c.ClientId,
                   //JoinType.Left,s.UserId==u.UserId,
                   JoinType.Left,s.ModifiedBy == u.UserId
                 })
                 //.Where((s, c, u, w) => w.UserId == userId)
                 .Select((s, c, u) => new
                 {
                     s.DeviceId,
                     c.ClientArea,
                     c.ClientName,
                     s.DeviceName,
                     s.DeviceSerialNum,
                     s.DeviceIp,
                     //s.AliDeviceName,
                     //s.AliDeviceSecret,
                     s.DeviceType,
                     //s.AliProductKey,
                     //s.AliIotId,
                     CName = u.UserNickname,
                     s.CreateDate,
                     //UName = u.UserNickname,
                     s.ModifiedDate,
                 }).MergeTable().Where((s) => s.DeviceId != null).OrderBy(t => t.ModifiedDate);

            if (!bootstrap.search.IsEmpty())
            {
                query.Where((s) => s.DeviceName.Contains(bootstrap.search));
            }

            if (!bootstrap.datemin.IsEmpty() && !bootstrap.datemax.IsEmpty())
            {
                query.Where(s => s.CreateDate > bootstrap.datemin.ToDateTimeB() && s.CreateDate <= bootstrap.datemax.ToDateTimeE());
            }
            if (bootstrap.order.Equals("desc", StringComparison.OrdinalIgnoreCase))
            {
                query.OrderBy($"MergeTable.{bootstrap.sort} desc");
            }
            else
            {
                query.OrderBy($"MergeTable.{bootstrap.sort} asc");
            }
            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return Bootstrap.GridData(list, totalNumber).JilToJson();
        }


        public string PageRegisterList(Bootstrap.BootstrapParams bootstrap, long userId)
        {
            int totalNumber = 0;
            if (bootstrap.offset != 0)
            {
                bootstrap.offset = bootstrap.offset / bootstrap.limit + 1;
            }

            var query = _client.Queryable<DeviceInfo, Sys_user>
                ((s, u) => new object[] {
                   //JoinType.Left,s.ClientId==c.ClientId,
                   JoinType.Left,s.UserId==u.UserId,
                   //JoinType.Left,c.ClientId==w.ClientId
                 }).Select((s, u) => new
                 {
                     //DeviceId = s.DeviceId.ToString(),
                     //c.ClientArea,
                     //c.ClientName,
                     //s.DeviceName,
                     s.DeviceId,
                     s.DeviceSerialNum,
                     s.DeviceIp,
                     s.AliDeviceName,
                     s.AliDeviceSecret,
                     s.DeviceType,
                     s.AliProductKey,
                     s.AliIotId,
                     CName = u.UserNickname,
                     s.CreateDate,
                     //UName = u.UserNickname,
                     s.ModifiedDate,
                     s.Description,
                     s.DeviceBatch
                 }).MergeTable().Where((s) => s.DeviceId > 0).OrderBy(t => t.DeviceBatch);

            //if (!bootstrap.search.IsEmpty())
            //{
            //    query.Where((s) => s.DeviceName.Contains(bootstrap.search));
            //}

            if (!bootstrap.datemin.IsEmpty() && !bootstrap.datemax.IsEmpty())
            {
                query.Where(s => s.CreateDate > bootstrap.datemin.ToDateTimeB() && s.CreateDate <= bootstrap.datemax.ToDateTimeE());
            }
            if (bootstrap.order.Equals("desc", StringComparison.OrdinalIgnoreCase))
            {
                query.OrderBy($"MergeTable.{bootstrap.sort} desc");
            }
            else
            {
                query.OrderBy($"MergeTable.{bootstrap.sort} asc");
            }
            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return Bootstrap.GridData(list, totalNumber).JilToJson();
        }

        public bool UpdateDeviceInfo(DeviceInfo deviceinfo)
        {
            return _repository.Update(deviceinfo,it=>new {it.DeviceName,it.ClientId });

        }
    }
}